﻿using BlazorAdmin.Tcp.Protocol;

namespace BlazorAdmin.Tcp.Server
{
    /// <summary>
    /// Tcp配置参数
    /// </summary>
    public class TcpOptions
    {
        /// <summary>
        /// Tcp服务监听的端口，默认为：18880
        /// </summary>
        public int Port { get; set; } = 18880;

        /// <summary>
        /// Tcp服务接收消息的缓存大小，默认1KB
        /// 注意：不能超过Windows最大缓存8KB，超过自动覆盖设置为0，造成所有Socket客户端连不上
        /// </summary>
        public int ReceiveBufferSize { get; set; } = 1024;

        /// <summary>
        /// 支持的最大连接数，默认1万个连接
        /// </summary>
        public int MaxConnections { get; set; } = 10000;

        /// <summary>
        /// 处理通讯协议的组件
        /// </summary>
        public List<ProtocolBase>? Handles { get; set; }

        /// <summary>
        /// 获取协议解析组件
        /// </summary>
        /// <param name="buffer">数据缓存区</param>
        /// <param name="start">数据起始位置</param>
        /// <param name="count">数据总量</param>
        /// <returns></returns>
        internal ProtocolBase? GetHandler(byte[]? buffer, int start, int count)
        {
            if (null == buffer || null == Handles || Handles.Count < 1)
                return default;

            return Handles.FirstOrDefault(h => null != h.Analysis(buffer, start, count));
        }
    }
}
